﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

/*
	Todo:
	0. Test ef on a variety of classes
		- ideally need a mapping of invalid chars to replacements eg $ -> Usd
	1. implement other code gen
		LINQ class only and class plus test
			- options implemented.
		PetaPoco everything
			OK.
	* 3. remove tabs from all templates
	* 3a. Enable overwrite of class name. Explain about plurals.
	* 4. enable ef group only if ef and full options
	* 5. implement ef config selections

*/
namespace SqlServerDriver
{
	public partial class ClassBuilderForm : Form
	{
		public ClassBuilderForm(String tableName, SqlServerDataSource source)
		{
			InitializeComponent();
			TableName = tableName;
			this.source = source;
		}

		public String TableName {get; set;}
		SqlServerDataSource source;
		SqlTableSchema schema;

		static private int classSelection = 0;

		private void ClassBuilderForm_Load(object sender, EventArgs e)
		{
			lblHeading.Text = lblHeading.Text.Replace("XXX", TableName);

			txtClassName.Text = TableGeneratorHelpers.MakePascalCaseName(TableName, true);

			listOptions.Items.Add("Entity Framework");
			listOptions.Items.Add("LINQ2SQL");
			listOptions.Items.Add("PetaPoco");
			listOptions.SelectedIndex = classSelection;

			radioClassOnly.Checked = true;
			radioFluent1.Checked = true;
			//GenerateClass();
		}

		private void LlistOptions_SelectedIndexChanged(object sender, EventArgs e) {	GenerateClass(); }
		private void Radio_CheckedChanged(object sender, EventArgs e) {	GenerateClass(); }
		private void TxtClassName_TextChanged(object sender, EventArgs e) {GenerateClass(); }

		// generate class (and fix-up controls)
		private void GenerateClass()
		{
			if (this.schema == null)
				this.schema = this.source.MakeSchemaFor(TableName);

			String className = txtClassName.Text.Replace(" ","").Trim();  // rudimentary validation
			if (className.Length == 0)
				className = TableGeneratorHelpers.MakePascalCaseName(TableName, true); // restore to default

			int codeScope = (radioClassOnly.Checked) ? 0 :
							(radioClassAndSample.Checked) ? 1 :
							(radioFull.Checked) ? 2 : 0;
			EFClassGenerator.ConfigStyle cfgOption = (radioAttributes.Checked) ? EFClassGenerator.ConfigStyle.Annotations :
												(radioFluent1.Checked) ? EFClassGenerator.ConfigStyle.FluentCentral :
												(radioFluent2.Checked) ? EFClassGenerator.ConfigStyle.FluentIndividual :
																		 EFClassGenerator.ConfigStyle.Annotations;
			bool enableEFoptions = false;
			String code = "";
			if (listOptions.SelectedIndex == 0)
			{
				EFClassGenerator scripter = new EFClassGenerator(className, TableName, this.schema);
				code = scripter.GenerateCode(this.source.ConnectionString, codeScope, cfgOption);
				enableEFoptions = true;
			}
			if (listOptions.SelectedIndex == 1)
			{
				LinqTableGenerator scripter = new LinqTableGenerator(TableName, this.schema);
				code = scripter.GenerateCode(this.source.ConnectionString, codeScope);
			}
			if (listOptions.SelectedIndex == 2)
			{
				PetaPocoTableGenerator scripter = new PetaPocoTableGenerator(TableName, this.schema);
				code = scripter.GenerateCode(this.source.ConnectionString, codeScope);
			}
			txtCode.Text = code.Replace("\t", "    ");
			SetEfOptions(enableEFoptions);
		}

		private void SetEfOptions(bool enableEFoptions)
		{
			this.groupEF.Enabled = enableEFoptions;
			this.radioAttributes.Enabled =  enableEFoptions;
			this.radioFluent1.Enabled =  enableEFoptions;
			this.radioFluent2.Enabled =  enableEFoptions;
		}

		private void BtnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void BtnCopy_Click(object sender, EventArgs e)
		{
			Clipboard.SetText(txtCode.Text);
		}
	}
}
